package org.itheima.game.org.tank.game.model

import org.itheima.game.org.tank.game.Config
import org.itheima.game.org.tank.game.business.*
import org.itheima.game.org.tank.game.enum.Direction
import org.itheima.kotlin.game.core.Painter
import kotlin.random.Random

/**
 * description ：敌方坦克
 * author : chenmengjia
 * email : cmjsp1990@163.com
 * date : 2020/3/15 10:09
 */
class Enemy(override var x: Int, override var y: Int) : Movable, AutoMovable, Blockable, AutoShot, Sufferable,
    Destroyable {


    override var blood: Int = 2


    override var speed: Int = 8
    override var currentDirection: Direction = Direction.DOWN
    private var badDirection: Direction? = null
    private var lastShotTime = 0L
    private var lastMoveTime = 0L
    private var moveFrequency = 50L
    private var shotFrequency = 800L
    override fun autoMove() {
        val current = System.currentTimeMillis()
        if (current - lastMoveTime < moveFrequency) {
            return
        }
        lastMoveTime = current
        if (currentDirection == badDirection) {
            //改变自己方向
            currentDirection = randomDirection(badDirection)
            return
        }
        when (currentDirection) {
            Direction.UP -> y -= speed
            Direction.DOWN -> y += speed
            Direction.LEFT -> x -= speed
            Direction.RIGHT -> x += speed

        }
        //越界判断
        if (x <= 0) x = 0
        if (x > Config.gameWidth - width) x = Config.gameWidth - width
        if (y < 0) y = 0
        if (x > Config.gameHeight - height) y = Config.gameHeight - height
    }

    private fun randomDirection(badDirection: Direction?): Direction {
        val i = Random.nextInt(4)
        val direction = when (i) {
            0 -> Direction.UP
            1 -> Direction.DOWN
            2 -> Direction.LEFT
            3 -> Direction.RIGHT
            else -> Direction.RIGHT
        }
        if (direction == badDirection) {
            randomDirection(badDirection)
        }
        return direction

    }


//    override fun willCollision(blockable: Blockable): Direction? {
//        return null
//    }

    override fun notifyCollision(direction: Direction?, blockable: Blockable?) {
        badDirection = direction

    }

    override var width: Int = Config.block

    override var height: Int = Config.block


    override fun draw() {
        var imagePath = when (currentDirection) {
            Direction.UP -> "img/enemy_1_u.gif"
            Direction.DOWN -> "img/enemy_1_d.gif"
            Direction.LEFT -> "img/enemy_1_l.gif"
            Direction.RIGHT -> "img/enemy_1_r.gif"
        }
        Painter.drawImage(imagePath, x, y)
    }

    override fun autoShot(): View? {
        val current = System.currentTimeMillis()
        if (current - lastShotTime < shotFrequency) {
            return null
        }
        lastShotTime = current
        return Bullet(this, currentDirection) { bulletWidth, bulletHeight ->
            var tankY = y
            var tankX = x
            var tankWidth = width
            var tankHeight = height
            var bulletX = 0
            var bulletY = 0

            when (currentDirection) {
                Direction.UP -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY - bulletHeight / 2

                }
                Direction.DOWN -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY + tankHeight - bulletHeight / 2

                }
                Direction.LEFT -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2

                }
                Direction.RIGHT -> {
                    bulletX = tankX + tankWidth - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2

                }

            }
            Pair(bulletX, bulletY)
        }
    }

    override fun notifySuffer(attackable: Attackable): Array<View>? {
        //敌方自己打自己，不掉血，不爆炸
        if (attackable.owner is Enemy) {
            return null
        }
        blood -= attackable.attackPower
        return arrayOf(Blast(x, y))

    }

    override fun isDestroyed(): Boolean = blood <= 0
}